#include "factorytext.h"
using namespace std;
TextFactory::TextFactory()
{
	reserve_extensions.insert(std::make_pair("txt", & TextFactory::newTxtText));
	reserve_extensions.insert(std::make_pair("xml", & TextFactory::newXmlText));
}
Text &  TextFactory::giveText(std::string const & file_name) const 
{
	std::string ext = findExtension(file_name);
	map<std::string, TextFuncP >::const_iterator f = reserve_extensions.find(ext);
	if(f != reserve_extensions.end())
	{
		return (this->*(f->second))();
	}
	else
	{
		throw logic_error("File extension is not registred");
	}
}
string TextFactory::findExtension(string const & file_name) const
{
	if(file_name.size() == 0)
	{
		throw length_error("File name size is 0");
	}
	for(size_t i = file_name.size() - 1; ; --i)
	{
		if(file_name[i] == '.')
		{
			return file_name.substr(i+1);
		}
		if(i == 0) break;
	}
	throw logic_error("File name without extension");
}
Text &  TextFactory::newTxtText() const
{
	TxtText * text = new TxtText();
	return *text;
}
Text & TextFactory::newXmlText() const
{
	XmlText * text = new XmlText();
	return *text;
}